home *** CD-ROM | disk | FTP | other *** search
/ CD Ware Multimedia 1995 May / cd Ware (Juegos) Epimundo.iso / DOS / C / CMATH.ZIP / POWI.C < prev    next >
Encoding:
C/C++ Source or Header  |  1989-03-21  |  2.6 KB  |  160 lines

  1. /*                            powi.c
  2.  *
  3.  *    Real raised to integer power
  4.  *
  5.  *
  6.  *
  7.  * SYNOPSIS:
  8.  *
  9.  * double x, y, powi();
  10.  * int n;
  11.  *
  12.  * y = powi( x, n );
  13.  *
  14.  *
  15.  *
  16.  * DESCRIPTION:
  17.  *
  18.  * Returns argument x raised to the nth power.
  19.  * The routine efficiently decomposes n as a sum of powers of
  20.  * two. The desired power is a product of two-to-the-kth
  21.  * powers of x.  Thus to compute the 32767 power of x requires
  22.  * 28 multiplications instead of 32767 multiplications.
  23.  *
  24.  *
  25.  *
  26.  * ACCURACY:
  27.  *
  28.  *
  29.  *                      Relative error:
  30.  * arithmetic   x domain   n domain  # trials      peak         rms
  31.  *    DEC       .04,26     -26,26    100000       2.7e-16     4.3e-17
  32.  *    IEEE      .04,26     -26,26     50000       2.0e-15     3.8e-16
  33.  *    IEEE        1,2    -1022,1023   50000       8.6e-14     1.6e-14
  34.  *
  35.  * Returns MAXNUM on overflow, zero on underflow.
  36.  *
  37.  */
  38.  
  39. /*                            powi.c    */
  40.  
  41. /*
  42. Cephes Math Library Release 2.1:  January, 1989
  43. Copyright 1984, 1987, 1989 by Stephen L. Moshier
  44. Direct inquiries to 30 Frost Street, Cambridge, MA 02140
  45. */
  46.  
  47. #include "mconf.h"
  48. extern double MAXNUM, MAXLOG, MINLOG, LOGE2;
  49.  
  50. double powi( x, nn )
  51. double x;
  52. int nn;
  53. {
  54. int n, e, sign, asign, lx;
  55. short *p;
  56. double w, y, s;
  57. double log(), frexp();
  58.  
  59. if( x == 0.0 )
  60.     {
  61.     if( nn == 0 )
  62.         return( 1.0 );
  63.     else if( nn < 0 )
  64.         return( MAXNUM );
  65.     else
  66.         return( 0.0 );
  67.     }
  68.  
  69. if( nn == 0 )
  70.     return( 1.0 );
  71.  
  72.  
  73. if( x < 0.0 )
  74.     {
  75.     asign = -1;
  76.     x = -x;
  77.     }
  78. else
  79.     asign = 0;
  80.  
  81.  
  82. if( nn < 0 )
  83.     {
  84.     sign = -1;
  85.     n = -nn;
  86. /*
  87.     x = 1.0/x;
  88. */
  89.     }
  90. else
  91.     {
  92.     sign = 0;
  93.     n = nn;
  94.     }
  95.  
  96. /* Overflow detection */
  97.  
  98. /* Calculate approximate logarithm of answer */
  99. s = frexp( x, &lx );
  100. e = (lx - 1)*n;
  101. if( (e == 0) || (e > 64) || (e < -64) )
  102.     {
  103.     s = (s - 7.0710678118654752e-1) / (s +  7.0710678118654752e-1);
  104.     s = (2.9142135623730950 * s - 0.5 + lx) * nn * LOGE2;
  105.     }
  106. else
  107.     {
  108.     s = LOGE2 * e;
  109.     }
  110.  
  111. if( s > MAXLOG )
  112.     {
  113.     mtherr( "powi", OVERFLOW );
  114.     y = MAXNUM;
  115.     goto done;
  116.     }
  117.  
  118. if( s < MINLOG )
  119.     return(0.0);
  120.  
  121. /* Handle tiny denormal answer, but with less accuracy
  122.  * since roundoff error in 1.0/x will be amplified.
  123.  * The precise demarcation should be the gradual underflow threshold.
  124.  */
  125. if( s < (-MAXLOG+2.0) )
  126.     {
  127.     x = 1.0/x;
  128.     sign = 0;
  129.     }
  130.  
  131. /* First bit of the power */
  132. if( n & 1 )
  133.     y = x;
  134.         
  135. else
  136.     {
  137.     y = 1.0;
  138.     asign = 0;
  139.     }
  140.  
  141. w = x;
  142. n >>= 1;
  143. while( n )
  144.     {
  145.     w = w * w;    /* arg to the 2-to-the-kth power */
  146.     if( n & 1 )    /* if that bit is set, then include in product */
  147.         y *= w;
  148.     n >>= 1;
  149.     }
  150.  
  151.  
  152. done:
  153.  
  154. if( asign )
  155.     y = -y; /* odd power of negative number */
  156. if( sign )
  157.     y = 1.0/y;
  158. return(y);
  159. }
  160.